//-----------------------------------------------------------------------------
#include <QIcon>
#include <QSqlRecord>
#include <QPluginLoader>
#include "CConnectionFrm.h"
#include "CConfigFrm.h"
#include "CPaysFrm.h"
#include "CModeReglementFrm.h"
#include "CConditionReglementFrm.h"
#include "CFamilleArticleFrm.h"
#include "CUnitesFrm.h"
#include "CNotesFrm.h"
#include "CStatusPaieFrm.h"
#include "CArticleFrm.h"
#include "CDevisFrm.h"
#include "CCommandesFrm.h"
#include "CBLsFrm.h"
#include "CFacturesFrm.h"
#include "CTextAfficheFrm.h"
#include "CAbonnementEMailingFrm.h"
#include "CActionFrm.h"
#include "CChatFrm.h"
#include "CPontComptableFrm.h"
#include "CTvaFrm.h"
#include "CMainFrm.h"
#include "version.h"
#include "CMySQL.h"
#include "CHelp.h"
#include "CCrypt.h"
#include "CMyMenu.h"
#include "CGlobal.h"
#include "CTomcatClient.h"
//------------------------------------------------------------------------------
CMainFrm::CMainFrm(QWidget *parent, QString profilName) : QMainWindow(parent) {
	profils=CProfils::getInstance();
	if(profilName != "") {
		profils->setCurrentProfil(profilName);
	}
	
	setWindowIcon(QIcon(":coMexpert/images/ares.png"));
	
	mdiArea=new QMdiArea();
	setCentralWidget(mdiArea);
	
	createActions();
	createMenus();
	createToolBar();
}
//------------------------------------------------------------------------------
void CMainFrm::createActions(void)
{
	actConnect=new QAction(QIcon(":coMexpert/images/connect_creating.png"), tr("&Connexion"), this);
	actConnect->setIconVisibleInMenu(true);
	connect(actConnect, SIGNAL(triggered()), this, SLOT(authentification()));
	
	actConfigConnection=new QAction(QIcon(":coMexpert/images/configure.png"), tr("C&onfiguration"), this);
	actConfigConnection->setIconVisibleInMenu(true);
	connect(actConfigConnection, SIGNAL(triggered()), this, SLOT(configure()));
	
	actQuit=new QAction(QIcon(":coMexpert/images/exit.png"), tr("&Quitter"), this);
	actQuit->setIconVisibleInMenu(true);
	connect(actQuit, SIGNAL(triggered()), this, SLOT(close()));
	
	actAboutQt=new QAction(QIcon(":coMexpert/images/qt.png"), tr("&A propos de Qt"), this);
	actAboutQt->setIconVisibleInMenu(true);
	connect(actAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
	
	actMySQLVersion=new QAction(QIcon(":coMexpert/images/db.png"), tr("&Version du serveur MySQL"), this);
	actMySQLVersion->setEnabled(false);
	actMySQLVersion->setIconVisibleInMenu(true);
	connect(actMySQLVersion, SIGNAL(triggered()), this, SLOT(versionMySQL()));
	
	actServletVersion=new QAction(QIcon(":coMexpert/images/java_src.png"), tr("V&ersion de la servlet"), this);
	actServletVersion->setEnabled(false);
	actServletVersion->setIconVisibleInMenu(true);
	connect(actServletVersion, SIGNAL(triggered()), this, SLOT(versionServlet()));
	
	actClient=new QAction(QIcon(":coMexpert/images/tux.png"), tr("&Client"), this);
	actClient->setEnabled(false);
	actClient->setIconVisibleInMenu(true);
	connect(actClient, SIGNAL(triggered()), this, SLOT(loadFicheClient()));
	
	actArticle=new QAction(QIcon(":coMexpert/images/camera.png"), tr("&Article"), this);
	actArticle->setEnabled(false);
	actArticle->setIconVisibleInMenu(true);
	connect(actArticle, SIGNAL(triggered()), this, SLOT(loadFicheArticle()));
	
	actDevis=new QAction(QIcon(":coMexpert/images/colorize.png"), tr("&Devis"), this);
	actDevis->setEnabled(false);
	actDevis->setIconVisibleInMenu(true);
	connect(actDevis, SIGNAL(triggered()), this, SLOT(loadDevis()));

	actCommandes=new QAction(QIcon(":coMexpert/images/package_games_kids.png"), tr("C&ommandes"), this);
	actCommandes->setEnabled(false);
	actCommandes->setIconVisibleInMenu(true);
	connect(actCommandes, SIGNAL(triggered()), this, SLOT(loadCommandes()));

	actBLs=new QAction(QIcon(":coMexpert/images/outbox1.png"), tr("&Bons de livraison"), this);
	actBLs->setEnabled(false);
	actBLs->setIconVisibleInMenu(true);
	connect(actBLs, SIGNAL(triggered()), this, SLOT(loadBLs()));

	actFactures=new QAction(QIcon(":coMexpert/images/money.png"), tr("&Factures / Avoirs"), this);
	actFactures->setEnabled(false);
	actFactures->setIconVisibleInMenu(true);
	connect(actFactures, SIGNAL(triggered()), this, SLOT(loadFactures()));
	
	actPontsComptable=new QAction(QIcon(":coMexpert/images/business.png"), tr("&Ponts comptable"), this);
	actPontsComptable->setEnabled(false);
	actPontsComptable->setIconVisibleInMenu(true);
	connect(actPontsComptable, SIGNAL(triggered()), this, SLOT(loadPontsComptable()));

	actProspect=new QAction(QIcon(":coMexpert/images/katuberling.png"), tr("&Prospect"), this);
	actProspect->setEnabled(false);
	actProspect->setIconVisibleInMenu(true);
	connect(actProspect, SIGNAL(triggered()), this, SLOT(loadProspect()));
	
	actCA=new QAction(QIcon(":coMexpert/images/kchart_chrt.png"), tr("&Chiffre d'affaire"), this);
	actCA->setEnabled(false);
	actCA->setIconVisibleInMenu(true);
	connect(actCA, SIGNAL(triggered()), this, SLOT(loadCA()));
	
	actPays=new QAction(QIcon(":coMexpert/images/agt_internet.png"), tr("&Pays"), this);
	actPays->setEnabled(false);
	actPays->setIconVisibleInMenu(true);
	connect(actPays, SIGNAL(triggered()), this, SLOT(loadFichePays()));
	
	actModeReglement=new QAction(QIcon(":coMexpert/images/money.png"), tr("&Modes de règlement"), this);
	actModeReglement->setEnabled(false);
	actModeReglement->setIconVisibleInMenu(true);
	connect(actModeReglement, SIGNAL(triggered()), this, SLOT(loadModeReglement()));
	
	actConditionReglement=new QAction(QIcon(":coMexpert/images/1day.png"), tr("&Conditions de règlement"), this);
	actConditionReglement->setEnabled(false);
	actConditionReglement->setIconVisibleInMenu(true);
	connect(actConditionReglement, SIGNAL(triggered()), this, SLOT(loadConditionReglement()));
	
	actFamilleArticle=new QAction(QIcon(":coMexpert/images/babelfish.png"), tr("&Famille article"), this);
	actFamilleArticle->setEnabled(false);
	actFamilleArticle->setIconVisibleInMenu(true);
	connect(actFamilleArticle, SIGNAL(triggered()), this, SLOT(loadFamilleArticle()));

	actUnites=new QAction(QIcon(":coMexpert/images/attach.png"), tr("&Unites"), this);
	actUnites->setEnabled(false);
	actUnites->setIconVisibleInMenu(true);
	connect(actUnites, SIGNAL(triggered()), this, SLOT(loadUnites()));

	actNotes=new QAction(QIcon(":coMexpert/images/playsound.png"), tr("&Notes"), this);
	actNotes->setEnabled(false);
	actNotes->setIconVisibleInMenu(true);
	connect(actNotes, SIGNAL(triggered()), this, SLOT(loadNotes()));

	actStatusPaie=new QAction(QIcon(":coMexpert/images/blender.png"), tr("&Status des paies"), this);
	actStatusPaie->setEnabled(false);
	actStatusPaie->setIconVisibleInMenu(true);
	connect(actStatusPaie, SIGNAL(triggered()), this, SLOT(loadStatusPaie()));

	actAction=new QAction(QIcon(":coMexpert/images/aktion.png"), tr("&Actions"), this);
	actAction->setEnabled(false);
	actAction->setIconVisibleInMenu(true);
	connect(actAction, SIGNAL(triggered()), this, SLOT(loadAction()));

	actAbonnementEMailing=new QAction(QIcon(":coMexpert/images/mail_generic.png"), tr("&Abonnements au eMailing"), this);
	actAbonnementEMailing->setEnabled(false);
	actAbonnementEMailing->setIconVisibleInMenu(true);
	connect(actAbonnementEMailing, SIGNAL(triggered()), this, SLOT(loadAbonnementEMailing()));

	actChat=new QAction(QIcon(":coMexpert/images/chat.png"), tr("&Discussion instantané"), this);
	actChat->setEnabled(false);
	actChat->setIconVisibleInMenu(true);
	connect(actChat, SIGNAL(triggered()), this, SLOT(loadChat()));
	
	actNoteDeVersion=new QAction(QIcon(":coMexpert/images/info.png"), tr("&Notes de version"), this);
	actNoteDeVersion->setIconVisibleInMenu(true);
	connect(actNoteDeVersion, SIGNAL(triggered()), this, SLOT(showNotesDeVersion()));

	actHelp=new QAction(QIcon(":coMexpert/images/help.png"), tr("&Manuel de coMexpert"), this);
	actHelp->setIconVisibleInMenu(true);
	connect(actHelp, SIGNAL(triggered()), this, SLOT(onInvokeHelp()));
	
	actSociete=new QAction(QIcon(":coMexpert/images/gohome.png"), tr("&Sociétés"), this);
	actSociete->setEnabled(false);
	actSociete->setIconVisibleInMenu(true);
	connect(actSociete, SIGNAL(triggered()), this, SLOT(loadSociete()));
	
	actTva=new QAction(QIcon(":coMexpert/images/agt_virus.png"), tr("&Tva"), this);
	actTva->setIconVisibleInMenu(true);
	actTva->setEnabled(false);
	connect(actTva, SIGNAL(triggered()), this, SLOT(loadTva()));
}
//------------------------------------------------------------------------------
void CMainFrm::createMenus(void)
{
	mnuFile=new CMyMenu(tr("&Fichiers"));
	menuBar()->addMenu(mnuFile);
	mnuFile->addAction(actConnect);
	mnuFile->addAction(actConfigConnection);
	mnuFile->addSeparator();
	mnuFile->addAction(actQuit);
	
	mnuVente=menuBar()->addMenu(tr("&Vente"));
	mnuVente->addAction(actClient);
	mnuVente->addAction(actArticle);
	mnuVente->addAction(actDevis);
	mnuVente->addAction(actCommandes);
	mnuVente->addAction(actBLs);
	mnuVente->addAction(actFactures);
	mnuVente->addAction(actProspect);
	mnuVente->addAction(actPontsComptable);
	mnuVente->addSeparator();
	mnuVente->addAction(actCA);

	mnuAccessoire=menuBar()->addMenu(tr("&Accessoires"));
	mnuAccessoire->addAction(actSociete);
	mnuAccessoire->addAction(actPays);
	mnuAccessoire->addAction(actModeReglement);
	mnuAccessoire->addAction(actConditionReglement);
	mnuAccessoire->addAction(actFamilleArticle);
	mnuAccessoire->addAction(actUnites);
	mnuAccessoire->addAction(actNotes);
	mnuAccessoire->addAction(actStatusPaie);
	mnuAccessoire->addAction(actAction);
	mnuAccessoire->addAction(actTva);

	mnuDivers=menuBar()->addMenu(tr("&Divers"));
	mnuDivers->addAction(actAbonnementEMailing);
	mnuDivers->addAction(actChat);
	
	mnuHelp=menuBar()->addMenu(tr("?"));
	mnuHelp->addAction(actHelp);
	mnuHelp->addAction(actNoteDeVersion);
	mnuHelp->addAction(actAboutQt);
	mnuHelp->addSeparator();
	mnuHelp->addAction(actMySQLVersion);
	mnuHelp->addAction(actServletVersion);
}
//------------------------------------------------------------------------------
void CMainFrm::createToolBar(void)
{
	mainToolBar=addToolBar(tr("main"));
	mainToolBar->addAction(actClient);
	mainToolBar->addAction(actArticle);
	mainToolBar->addAction(actDevis);
	mainToolBar->addAction(actCommandes);
	mainToolBar->addAction(actBLs);
	mainToolBar->addAction(actFactures);
	mainToolBar->addAction(actProspect);
}
//------------------------------------------------------------------------------
void CMainFrm::closeEvent(QCloseEvent * event)
{
	if(closeMdiChild())
	{
		printf("A bientôt sur coMexpert\n");
		if(CMySQL::getInstance()->isConnect())
		{
			CMySQL::getInstance()->close();
		}
		profils->write();
		event->accept();
	}else
	{
		event->ignore();
	}
}
//------------------------------------------------------------------------------
QMainWindow * CMainFrm::findMdiChild(QString name)
{
	foreach(QMdiSubWindow *subWindow, mdiArea->subWindowList()) 
	{
		QMainWindow *mdiChild = qobject_cast<QMainWindow *>(subWindow->widget());
		if (mdiChild->objectName() == name)
			return mdiChild;
	}
	return NULL;
}
//------------------------------------------------------------------------------
bool CMainFrm::closeMdiChild(void)
{
	foreach(QMdiSubWindow *subWindow, mdiArea->subWindowList()) 
	{
		QMainWindow *mdiChild = qobject_cast<QMainWindow *>(subWindow->widget());
		
		if(!mdiChild->close()) return false;
	}
	return true;
}
//------------------------------------------------------------------------------
void CMainFrm::authentification(void) {
	CConnectionFrm *connectionFrm;
	QString versionMinLogiciel, versionBase;
	CProfil *profil=profils->getCurrentProfil();
	
	if(profil == 0) {
		return;
	}
	
	connectionFrm=new CConnectionFrm(profil->getJasper(), profil->getBase(), this);
	connectionFrm->setUtilisateur(profil->getUser());
	connectionFrm->setStoreMotDePasse(profil->isStorePassword());
	connectionFrm->setMotDePasse(CCrypt::decrypt(profil->getPassword()));
	connectionFrm->setModal(true);
	connectionFrm->exec();
	
	if(connectionFrm->result() == QDialog::Accepted) {
		setCursor(Qt::WaitCursor);
		
		CTomcatClient tomcatClient;
		QVariantMap config;
		
		if(tomcatClient.authenticate(connectionFrm->getUtilisateur(), connectionFrm->getMotDePasse())) {
			if(tomcatClient.getConfig(config)) {
				CGlobal::setConfig(config);
				if(!CMySQL::getInstance()->dbConnect(	config.value("MySQLHost").toString(), config.value("MySQLUser").toString(), 
														config.value("MySQLPassWord").toString(), config.value("MySQLDBName").toString())) {
					QMessageBox msgBox(	QMessageBox::Warning, tr("Erreur"), 
						tr("Impossible de se connecter à la base de données"), QMessageBox::Ok);
					msgBox.exec();
				}else {
					if(!CMySQL::getInstance()->versionOk(versionMinLogiciel, versionBase)) {
						QMessageBox msgBox(	QMessageBox::Warning, tr("Erreur"), 
							tr("La base requiert la version ")+versionMinLogiciel+tr(" du logiciel ! (")+VERSION_LOGICIEL+tr(")\n")+tr("Le logiciel requiert la version ")+VERSION_MIN_BASE+tr(" de la base ! (")+versionBase+tr(")"), QMessageBox::Ok);
						msgBox.exec();

						CMySQL::getInstance()->close();
					}else {
						actConnect->setEnabled(false);
						actClient->setEnabled(true);
						actArticle->setEnabled(true);
						actDevis->setEnabled(true);
						actCommandes->setEnabled(true);
						actBLs->setEnabled(true);
						actFactures->setEnabled(true);
						actPays->setEnabled(true);
						actModeReglement->setEnabled(true);
						actConditionReglement->setEnabled(true);
						actFamilleArticle->setEnabled(true);
						actUnites->setEnabled(true);
						actNotes->setEnabled(true);
						actStatusPaie->setEnabled(true);
						actAction->setEnabled(true);
						actMySQLVersion->setEnabled(true);
						actServletVersion->setEnabled(true);
						actAbonnementEMailing->setEnabled(true);
						actChat->setEnabled(true);
						actProspect->setEnabled(true);
						actCA->setEnabled(true);
						actPontsComptable->setEnabled(true);
						actSociete->setEnabled(true);
						actTva->setEnabled(true);
						
						profil->setUser(connectionFrm->getUtilisateur());
						profil->setStorePassword(connectionFrm->getStoreMotDePasse());
						profil->setPassword(CCrypt::crypt(connectionFrm->getMotDePasse()));
											
						statusBar()->showMessage(tr("Connecté au serveur ")+config.value("MySQLHost").toString()+tr(" avec l'utilisateur ")+profil->getUser()+tr(" (base de données: ")+config.value("MySQLDBName").toString()+tr(")"));
			
						CMySQL::getInstance()->setBase(config.value("MySQLDBName").toString());
						CMySQL::getInstance()->setUser(connectionFrm->getUtilisateur());
						
						CMySQL::getInstance()->execQuery("SET NAMES \"utf8\"");
						CMySQL::getInstance()->execQuery("SET CHARACTER SET utf8");
						
						CGlobal::setCurrentUser(connectionFrm->getUtilisateur());
					}
				}
			}
		}else {
			QMessageBox msgBox(	QMessageBox::Warning, tr("Erreur"), 
									tomcatClient.getLastError(), QMessageBox::Ok);
			msgBox.exec();
		}
		setCursor(Qt::ArrowCursor);
	}
	
	delete connectionFrm;
}	
//------------------------------------------------------------------------------
void CMainFrm::showNotesDeVersion(void) {
	CTextAfficheFrm *notesDeVersionFrm=new CTextAfficheFrm(this, ":coMexpert/notesDeVersion.html");
	
	notesDeVersionFrm->setWindowTitle(tr("Notes de version"));
	notesDeVersionFrm->setWindowIcon(QIcon(":coMexpert/images/info.png"));
	notesDeVersionFrm->exec();
	
	delete notesDeVersionFrm;
}
//------------------------------------------------------------------------------
void CMainFrm::loadDevis(int numero)
{
	CDevisFrm *devisFrm;
	
	setCursor(Qt::WaitCursor);
	if((devisFrm=(CDevisFrm *)findMdiChild("devisFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastDevis;
		
		profil->getLast("devisFrm", lastDevis);
		
		devisFrm=new CDevisFrm("devisFrm", lastDevis, this);
		connect(devisFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		devisFrm->setAttribute(Qt::WA_DeleteOnClose);
		devisFrm->setMdiSubWindow(mdiArea->addSubWindow(devisFrm));
	}
	if(numero != -1)
	{
		devisFrm->setNumero(numero);
	}
	mdiArea->setActiveSubWindow(devisFrm->getMdiSubWindow());
	devisFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadCommandes(int numero)
{
	CCommandesFrm *commandesFrm;
	
	setCursor(Qt::WaitCursor);
	if((commandesFrm=(CCommandesFrm *)findMdiChild("commandesFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastCommande;
		
		profil->getLast("commandesFrm", lastCommande);
		
		commandesFrm=new CCommandesFrm("commandesFrm", lastCommande, this);
		connect(commandesFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		commandesFrm->setAttribute(Qt::WA_DeleteOnClose);
		commandesFrm->setMdiSubWindow(mdiArea->addSubWindow(commandesFrm));
	}
	if(numero != -1)
	{
		commandesFrm->setNumero(numero);
	}
	mdiArea->setActiveSubWindow(commandesFrm->getMdiSubWindow());
	commandesFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadBLs(int numero)
{
	CBLsFrm *bLsFrm;
	
	setCursor(Qt::WaitCursor);
	
	if((bLsFrm=(CBLsFrm *)findMdiChild("bLsFrm")) == NULL) {	
		CProfil *profil=profils->getCurrentProfil();
		QString lastBl;
		
		profil->getLast("bLsFrm", lastBl);
		
		bLsFrm=new CBLsFrm("bLsFrm", lastBl, this);
		connect(bLsFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		bLsFrm->setAttribute(Qt::WA_DeleteOnClose);
		bLsFrm->setMdiSubWindow(mdiArea->addSubWindow(bLsFrm));
	}
	if(numero != -1) {
		bLsFrm->setNumero(numero);
	}
	mdiArea->setActiveSubWindow(bLsFrm->getMdiSubWindow());
	bLsFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadFactures(int numero)
{
	CFacturesFrm *facturesFrm;
	
	setCursor(Qt::WaitCursor);
	if((facturesFrm=(CFacturesFrm *)findMdiChild("facturesFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastFacture;
		
		profil->getLast("facturesFrm", lastFacture);
		facturesFrm=new CFacturesFrm("facturesFrm", lastFacture, this);
		connect(facturesFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		facturesFrm->setAttribute(Qt::WA_DeleteOnClose);
		facturesFrm->setMdiSubWindow(mdiArea->addSubWindow(facturesFrm));
	}
	if(numero != -1)
	{
		facturesFrm->setNumero(numero);
	}
	mdiArea->setActiveSubWindow(facturesFrm->getMdiSubWindow());
	facturesFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::configure(void)
{
	CConfigFrm *configFrm;
	
	configFrm=new CConfigFrm(profils, this, Qt::Dialog);
	
	configFrm->setWindowModality(Qt::WindowModal);
	configFrm->show();
}
//------------------------------------------------------------------------------
void CMainFrm::loadFicheClient(void)
{
	CClientFrm *clientFrm;
	
	setCursor(Qt::WaitCursor);
	if((clientFrm=(CClientFrm *)findMdiChild("clientFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastClient;
		
		profil->getLast("clientFrm", lastClient);
		
		clientFrm=new CClientFrm("clientFrm", lastClient, this);
		connect(clientFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		clientFrm->setAttribute(Qt::WA_DeleteOnClose);
		clientFrm->setMdiSubWindow(mdiArea->addSubWindow(clientFrm));
	}
	mdiArea->setActiveSubWindow(clientFrm->getMdiSubWindow());
	clientFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadFicheArticle(void)
{
	CArticleFrm *articleFrm;
	
	setCursor(Qt::WaitCursor);
	if((articleFrm=(CArticleFrm *)findMdiChild("articleFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastArticle;
		
		profil->getLast("articleFrm", lastArticle);
		
		articleFrm=new CArticleFrm("articleFrm", lastArticle, this);
		connect(articleFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		articleFrm->setAttribute(Qt::WA_DeleteOnClose);
		articleFrm->setMdiSubWindow(mdiArea->addSubWindow(articleFrm));
	}
	mdiArea->setActiveSubWindow(articleFrm->getMdiSubWindow());
	articleFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadProspect(void)
{
	CProspectFrm *prospectFrm;
	
	setCursor(Qt::WaitCursor);
	if((prospectFrm=(CProspectFrm *)findMdiChild("prospectFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastProspect;
		
		profil->getLast("prospectFrm", lastProspect);
		prospectFrm=new CProspectFrm("prospectFrm", lastProspect, this);
		connect(prospectFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		prospectFrm->setAttribute(Qt::WA_DeleteOnClose);
		prospectFrm->setMdiSubWindow(mdiArea->addSubWindow(prospectFrm));
	}
	mdiArea->setActiveSubWindow(prospectFrm->getMdiSubWindow());
	prospectFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadCA(void)
{
	QString url=CGlobal::getConfig().value("HTTPHost").toString()+"/"+CGlobal::md5Hash(CMySQL::getInstance()->getBase())+"/evolution.php";
	
	QDesktopServices::openUrl(QUrl(url));
}
//------------------------------------------------------------------------------
void CMainFrm::loadFichePays(void)
{
	CPaysFrm *paysFrm;
	
	setCursor(Qt::WaitCursor);
	if((paysFrm=(CPaysFrm *)findMdiChild("paysFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastPays;
		
		profil->getLast("paysFrm", lastPays);
		
		paysFrm=new CPaysFrm("paysFrm", lastPays, this);
		connect(paysFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		paysFrm->setAttribute(Qt::WA_DeleteOnClose);
		paysFrm->setMdiSubWindow(mdiArea->addSubWindow(paysFrm));
	}
	mdiArea->setActiveSubWindow(paysFrm->getMdiSubWindow());
	paysFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadModeReglement(void)
{
	CModeReglementFrm *modeReglementFrm;
	
	setCursor(Qt::WaitCursor);
	if((modeReglementFrm=(CModeReglementFrm *)findMdiChild("modeReglementFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastModeReglement;
		
		profil->getLast("modeReglementFrm", lastModeReglement);
		
		modeReglementFrm=new CModeReglementFrm("modeReglementFrm", lastModeReglement, this);
		connect(modeReglementFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		modeReglementFrm->setAttribute(Qt::WA_DeleteOnClose);
		modeReglementFrm->setMdiSubWindow(mdiArea->addSubWindow(modeReglementFrm));
	}
	mdiArea->setActiveSubWindow(modeReglementFrm->getMdiSubWindow());
	modeReglementFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadConditionReglement(void)
{
	CConditionReglementFrm *conditionReglementFrm;
	
	setCursor(Qt::WaitCursor);
	if((conditionReglementFrm=(CConditionReglementFrm *)findMdiChild("conditionReglementFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastConditionReglement;
		
		profil->getLast("conditionReglementFrm", lastConditionReglement);
		
		conditionReglementFrm=new CConditionReglementFrm("conditionReglementFrm", lastConditionReglement, this);
		connect(conditionReglementFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		conditionReglementFrm->setAttribute(Qt::WA_DeleteOnClose);
		conditionReglementFrm->setMdiSubWindow(mdiArea->addSubWindow(conditionReglementFrm));
	}
	mdiArea->setActiveSubWindow(conditionReglementFrm->getMdiSubWindow());
	conditionReglementFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadFamilleArticle(void)
{
	CFamilleArticleFrm *familleArticleFrm;
	
	setCursor(Qt::WaitCursor);
	if((familleArticleFrm=(CFamilleArticleFrm *)findMdiChild("familleArticleFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastFamilleArticle;
		
		profil->getLast("familleArticleFrm", lastFamilleArticle);
		
		familleArticleFrm=new CFamilleArticleFrm("familleArticleFrm", lastFamilleArticle, this);
		connect(familleArticleFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		familleArticleFrm->setAttribute(Qt::WA_DeleteOnClose);
		familleArticleFrm->setMdiSubWindow(mdiArea->addSubWindow(familleArticleFrm));
	}
	mdiArea->setActiveSubWindow(familleArticleFrm->getMdiSubWindow());
	familleArticleFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadAbonnementEMailing(void)
{
	CAbonnementEMailingFrm *abonnementEMailingFrm;
	
	setCursor(Qt::WaitCursor);
	if((abonnementEMailingFrm=(CAbonnementEMailingFrm *)findMdiChild("abonnementEMailingFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QMap<QString, QVariant> lastKeys;
		
		profil->getLast("abonnementEMailingFrm", lastKeys);
		
		abonnementEMailingFrm=new CAbonnementEMailingFrm("abonnementEMailingFrm", lastKeys, this);
		connect(abonnementEMailingFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		abonnementEMailingFrm->setAttribute(Qt::WA_DeleteOnClose);
		abonnementEMailingFrm->setMdiSubWindow(mdiArea->addSubWindow(abonnementEMailingFrm));
	}
	mdiArea->setActiveSubWindow(abonnementEMailingFrm->getMdiSubWindow());
	abonnementEMailingFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadChat(void)
{
	CChatFrm *chatFrm;

	if((chatFrm=(CChatFrm *)findChild<CChatFrm *>("chatFrm")) == NULL)
	{	
		chatFrm=new CChatFrm("chatFrm", 0, Qt::Dialog | Qt::WindowStaysOnTopHint);
		chatFrm->move(x()+size().width()-chatFrm->size().width(), y()+size().height()-chatFrm->size().height());
		chatFrm->setAttribute(Qt::WA_DeleteOnClose);
	}

	chatFrm->show();
}
//------------------------------------------------------------------------------
void CMainFrm::loadUnites(void)
{
	CUnitesFrm *unitesFrm;
	
	setCursor(Qt::WaitCursor);
	if((unitesFrm=(CUnitesFrm *)findMdiChild("unitesFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QMap<QString, QVariant> lastKeys;
	
		profil->getLast("unitesFrm", lastKeys);
		
		unitesFrm=new CUnitesFrm("unitesFrm", lastKeys, this);
		connect(unitesFrm, SIGNAL(quit(QString,QMap<QString, QVariant>)), this, SLOT(onQuitFrm(QString,QMap<QString, QVariant>)));
		unitesFrm->setAttribute(Qt::WA_DeleteOnClose);
		unitesFrm->setMdiSubWindow(mdiArea->addSubWindow(unitesFrm));
	}
	mdiArea->setActiveSubWindow(unitesFrm->getMdiSubWindow());
	unitesFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadNotes(void)
{
	CNotesFrm *notesFrm;
	
	setCursor(Qt::WaitCursor);
	if((notesFrm=(CNotesFrm *)findMdiChild("notesFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastNote;
		
		profil->getLast("notesFrm", lastNote);
		
		notesFrm=new CNotesFrm("notesFrm", lastNote, this);
		connect(notesFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		notesFrm->setAttribute(Qt::WA_DeleteOnClose);
		notesFrm->setMdiSubWindow(mdiArea->addSubWindow(notesFrm));
	}
	mdiArea->setActiveSubWindow(notesFrm->getMdiSubWindow());
	notesFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadStatusPaie(void)
{
	CStatusPaieFrm *statusPaieFrm;
	
	setCursor(Qt::WaitCursor);
	if((statusPaieFrm=(CStatusPaieFrm *)findMdiChild("statusPaieFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastStatusPaie;
		
		profil->getLast("statusPaieFrm", lastStatusPaie);
		
		statusPaieFrm=new CStatusPaieFrm("statusPaieFrm", lastStatusPaie, this);
		connect(statusPaieFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		statusPaieFrm->setAttribute(Qt::WA_DeleteOnClose);
		statusPaieFrm->setMdiSubWindow(mdiArea->addSubWindow(statusPaieFrm));
	}
	mdiArea->setActiveSubWindow(statusPaieFrm->getMdiSubWindow());
	statusPaieFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadAction(void)
{
	CActionFrm *actionFrm;
	
	setCursor(Qt::WaitCursor);
	if((actionFrm=(CActionFrm *)findMdiChild("actionFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QMap<QString, QVariant> lastKeys;
		
		profil->getLast("actionFrm", lastKeys);
		
		actionFrm=new CActionFrm("actionFrm", lastKeys, this);
		connect(actionFrm, SIGNAL(quit(QString,QMap<QString, QVariant>)), this, SLOT(onQuitFrm(QString,QMap<QString, QVariant>)));
		actionFrm->setAttribute(Qt::WA_DeleteOnClose);
		actionFrm->setMdiSubWindow(mdiArea->addSubWindow(actionFrm));
	}
	mdiArea->setActiveSubWindow(actionFrm->getMdiSubWindow());
	actionFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::versionMySQL(void) {
	CTomcatClient tomcatClient;
	QString version;
	
	if(tomcatClient.databaseVersion(version)) {
		QMessageBox msgBox(	QMessageBox::Information, tr("Version du serveur de base de données"), 
								version, QMessageBox::Ok);
		msgBox.exec();
	}else {
		QMessageBox msgBox(	QMessageBox::Critical , tr("Erreur"), 
							tomcatClient.getLastError(), QMessageBox::Ok);
		
		msgBox.exec();
	}
}
//------------------------------------------------------------------------------
void CMainFrm::versionServlet(void) {
	CTomcatClient tomcatClient;
	QString version;
	
	if(tomcatClient.servletVersion(version)) {
		QMessageBox msgBox(	QMessageBox::Information, tr("Version de la servlet"), 
								version, QMessageBox::Ok);
		msgBox.exec();
	}else {
		QMessageBox msgBox(	QMessageBox::Critical , tr("Erreur"), 
							tomcatClient.getLastError(), QMessageBox::Ok);
		
		msgBox.exec();
	}
}
//------------------------------------------------------------------------------
void CMainFrm::onInvokeHelp(void)
{
	CHelp::getInstance()->startHelp("setSource qthelp://alpes-ressources.com.coMexpert-"+VERSION_LOGICIEL+"/doc/index.html");
}
//------------------------------------------------------------------------------
void CMainFrm::onQuitFrm(QString frmName, QString value)
{
	CProfil *profil=profils->getCurrentProfil();
	profil->setLast(frmName, value);
}
//------------------------------------------------------------------------------
void CMainFrm::onQuitFrm(QString frmName, QMap<QString, QVariant> keyValues)
{
	CProfil *profil=profils->getCurrentProfil();
	
	profil->setLast(frmName, keyValues);
}
//------------------------------------------------------------------------------
void CMainFrm::loadSociete(void)
{
	CSocieteFrm *societeFrm;
	
	setCursor(Qt::WaitCursor);
	if((societeFrm=(CSocieteFrm *)findMdiChild("societeFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastSociete;
		
		profil->getLast("societeFrm", lastSociete);
		
		societeFrm=new CSocieteFrm("societeFrm", lastSociete, this);
		connect(societeFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		societeFrm->setAttribute(Qt::WA_DeleteOnClose);
		societeFrm->setMdiSubWindow(mdiArea->addSubWindow(societeFrm));
	}
	mdiArea->setActiveSubWindow(societeFrm->getMdiSubWindow());
	societeFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadPontsComptable(void)
{
	CPontComptableFrm *pontComptableFrm;
	
	setCursor(Qt::WaitCursor);
	if((pontComptableFrm=(CPontComptableFrm *)findMdiChild("pontComptableFrm")) == NULL)
	{	
		pontComptableFrm=new CPontComptableFrm("pontComptableFrm", this);
		pontComptableFrm->setAttribute(Qt::WA_DeleteOnClose);
		pontComptableFrm->setMdiSubWindow(mdiArea->addSubWindow(pontComptableFrm));
	}
	mdiArea->setActiveSubWindow(pontComptableFrm->getMdiSubWindow());
	pontComptableFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------
void CMainFrm::loadTva(void)
{
	CTvaFrm *tvaFrm;
	
	setCursor(Qt::WaitCursor);
	if((tvaFrm=(CTvaFrm *)findMdiChild("tvaFrm")) == NULL)
	{	
		CProfil *profil=profils->getCurrentProfil();
		QString lastTva;
		
		profil->getLast("societeFrm", lastTva);
		
		tvaFrm=new CTvaFrm("societeFrm", lastTva, this);
		connect(tvaFrm, SIGNAL(quit(QString,QString)), this, SLOT(onQuitFrm(QString,QString)));
		tvaFrm->setAttribute(Qt::WA_DeleteOnClose);
		tvaFrm->setMdiSubWindow(mdiArea->addSubWindow(tvaFrm));
	}
	mdiArea->setActiveSubWindow(tvaFrm->getMdiSubWindow());
	tvaFrm->showMaximized();
	setCursor(Qt::ArrowCursor);
}
//------------------------------------------------------------------------------

